import requests
from datetime import datetime, timedelta
from config import Config


class WeatherMonitor:
    @staticmethod
    def get_hourly_forecast():
        """获取24小时预报数据"""
        url = f"https://devapi.qweather.com/v7/weather/24h?location={Config.LOCATION_ID}&key={Config.QWEATHER_API_KEY}"
        return requests.get(url).json().get("hourly", [])

    @classmethod
    def check_tomorrow_morning(cls):
        """检查次日早晨天气"""
        tomorrow = datetime.now() + timedelta(days=1)
        target_times = [f"{tomorrow.date()} {h:02d}:00" for h in Config.MORNING_HOURS]

        rain_data = []
        for hour in cls.get_hourly_forecast():
            if hour["fxTime"][:13] in target_times:
                if "雨" in hour["text"] or float(hour["precip"]) > 0.1:
                    rain_data.append({
                        "time": hour["fxTime"][11:16],
                        "precip": hour["precip"],
                        "desc": hour["text"]
                    })
        return rain_data